<template>
	<view>
		<!-- <uni-captcha :focus="focusCaptchaInput" ref="captcha" scene="send-sms-code" v-model="captcha" /> -->
		<view class="box">
			<input 
			:focus="focusSmsCodeInput"
			@blur="focusSmsCodeInput = false"
			type="number" class="input-box"
			:inputBorder="false"
			v-model="code" maxlength="6"
			placeholder="请输入短信验证码">
			</input>
			<view class="short-code-btn" hover-class="hover" @click="start">
				<text class="inner-text" :class="reverseNumber==0?'inner-text-active':''">{{innerText}}</text>
			</view>
		</view>
		<button class="uni-btn send-btn" type="primary" @click="submit" style="width: 70%;margin-left: 15%;margin-top: 100rpx;">注销</button>
	</view>
</template>

<script>
	function debounce(func, wait) {
		let timer;
		wait = wait || 500;
		return function() {
			let context = this;
			let args = arguments;
			if (timer) clearTimeout(timer);
			let callNow = !timer;
			timer = setTimeout(() => {
				timer = null;
			}, wait)
			if (callNow) func.apply(context, args);
		}
	}
	/**
	 * sms-form 
	 * @description 获取短信验证码组件
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=
	 * @property {Number} count 倒计时时长 s
	 * @property {String} phone 手机号码
	 * @property {String} type = [login-by-sms|reset-pwd-by-sms|bind-mobile] 	验证码类型，用于防止不同功能的验证码混用，目前支持的类型login登录、register注册、bind绑定手机、unbind解绑手机
	 * @property {false} focusCaptchaInput = [true|false] 验证码输入框是否默认获取焦点
	 */
	export default {
		name: "uni-sms-form",
		model: {
			prop: 'modelValue',
			event: 'update:modelValue'
		},
		props: {
			event: ['update:modelValue'],
			/**
			 * 倒计时时长 s
			 */
			count: {
				type: [String, Number],
				default: 60
			},
			/**
			 * 手机号码
			 */
			phone: {
				type: [String, Number],
				default: ''
			},
			/*
				验证码类型，用于防止不同功能的验证码混用，目前支持的类型login登录、register注册、bind绑定手机、unbind解绑手机
			*/
			type: {
				type: String,
				default () {
					return 'login'
				}
			},
			/*
				验证码输入框是否默认获取焦点
			*/
		
		},
		data() {
			return {
				captcha: "",
				reverseNumber: 0,
				reverseTimer: null,
				modelValue: "",
				focusSmsCodeInput:false,
				code:""
			};
		},
		watch: {
			captcha(value, oldValue) {
				if (value.length == 4 && oldValue.length != 4) {
					this.start()
				}
			},
			modelValue(value) {
				// TODO 兼容 vue2
				this.$emit('input', value);
				// TODO　兼容　vue3
				this.$emit('update:modelValue', value)
			}
		},
		computed: {
			innerText() {
				if (this.reverseNumber == 0) return "获取短信验证码";
				return "重新发送" + '(' + this.reverseNumber + 's)';
			}
		},
		created() {
			this.initClick();
		},
		methods: {
			getImageCaptcha(focus) {
				this.$refs.captcha.getImageCaptcha(focus)
			},
			initClick() {
				this.start = debounce(() => {
					if (this.reverseNumber != 0) return;
					this.sendMsg();
				})
			},
			sendMsg() {
		
			 
				const uniIdCo = uniCloud.importObject("uni-id-co", {
					customUI: true
				})
			 
				 var that =this
				that.$http('user.smssend', {
					verificationCodeType: "LOGIN",
					userType:"MEMBER",
					verifyType:"MOBILE",
					value:uni.getStorageSync("userinfo").mobile,
				}).then(res => {
					//console.info(JSON.stringify(res))
					if (res.code === 1) {
						this.reverseNumber = Number(this.count);
					    this.getCode();
					}
					if (res.code == 0) {
					 
						that.$u.toast(res.msg);
					}
				});
							 
			},
			getCode() {
			 
				if (this.reverseNumber == 0) {
					clearTimeout(this.reverseTimer);
					this.reverseTimer = null;
					return;
				}
				this.reverseNumber--;
				this.reverseTimer = setTimeout(() => {
					this.getCode();
				}, 1000)
			},
			submit() { //完成并提交
				const uniIdCo = uniCloud.importObject("uni-id-co", {
					errorOptions: {
						type: 'toast'
					}
				})
				 
				if (this.code.length != 6) {
		 
					return uni.showToast({
						title: '请查验输入的验证码',
						icon: 'none'
					});
				}
			 
				var that = this
			that.$http(
				'user.clearuser', {
					mobile: uni.getStorageSync("userinfo").mobile,
					code:that.code
				},
				'保存中...'
			).then(res => {
				
			uni.removeStorageSync("UId");
			uni.removeStorageSync("token");
			uni.removeStorageSync("isLogin");
			uni.removeStorageSync("userinfo");
			uni.removeStorageSync("InvitationCode");
			this.$Router.back(2);
			
			});
			
			
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		position: relative;
		margin-top: 10px;
	}

	.short-code-btn {
		padding: 0;
		position: absolute;
		top: 0;
		right: 8px;
		width: 260rpx;
		max-width: 130px;
		height: 44px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
		
	}

	.inner-text {
		font-size: 14px;
		color: #AAAAAA;
	}

	.inner-text-active {
		color: #04498c;
	}

	.captcha {
		width: 350rpx;
	}

	.input-box {
		margin: 0;
		padding: 4px;
		background-color: #F8F8F8;
		font-size: 14px;
	}

	.box ::v-deep .content-clear-icon {
		margin-right: 100px;
	}

	.box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
	}
	.popup-captcha .uni-btn {
		margin: 0;
	}
</style>
